﻿/*
--------------------------------------
File: Batch.cs
Project: Themaopdracht 7
Author: Maarten Kroon

Description:
A batch that can perform several algoritms in a row
--------------------------------------
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace THO7_Team8
{
    public class Batch : VisionAlgorithm
    {
        public Batch(String name)
            : base(name)
        {
        }

        /// <summary>
        /// The batch, that performs all the algoritms
        /// </summary>
        /// <param name="sourceImage">The original image</param>
        /// <returns>The first character in the array</returns>
        public override Bitmap DoAlgorithm(Bitmap sourceImage)
        {
            //Creates a return image, this is needed because otherwise the pixelFormat will be wrong 
            Bitmap returnImage = new Bitmap(sourceImage);
            //originalArray is used to return the original colors after crop
            int[] originalArray = MyImage.BmpToArray(returnImage);

            //imageArray is used in all the algoritms
            int[] imageArray = new int[originalArray.Length];
            originalArray.CopyTo(imageArray, 0);

            //SimpleColorDetect checks for yellowpixels in the image
            SimpleColorDetectMonogrome simpleColorDetectMonochrome = new SimpleColorDetectMonogrome();
            simpleColorDetectMonochrome.DoAlgorithm(ref imageArray);

            //BlobDetection to filter small blobs, multiple blobs and blobs which do not have the right dimensions
            LicensePlateBlobDetection licensePlateBlobDetection = new LicensePlateBlobDetection();
            licensePlateBlobDetection.DoAlgorithm(ref imageArray, sourceImage.Width);

            //If there are no good blobs, try to increase the brightness
            if (BlobUtils.getGoodBlobListSize() < 1)
            {
                originalArray.CopyTo(imageArray, 0);
                AdjustRGB argb = new AdjustRGB();
                argb.multiplyRGBValues(ref imageArray, 2);
                simpleColorDetectMonochrome.DoAlgorithm(ref imageArray);
                licensePlateBlobDetection.DoAlgorithm(ref imageArray, sourceImage.Width);
            }

            Point[] myPoint = BlobUtils.getCoordinates(ref imageArray, sourceImage.Width);

            //Crop license plate and returns the original colors
            //This is needed to return the small black spaces in letters
            //Returns a Bitmap because the size changes
            CropLicenseplateColoured cropLicensePlateColored = new CropLicenseplateColoured();
            Bitmap croppedBitmap = cropLicensePlateColored.DoAlgorithm(ref imageArray, ref originalArray, sourceImage.Width);

            //croppedArray is the array to be used in the next algoritms
            int[] croppedArray = MyImage.BmpToArray(croppedBitmap);

            //Make monochrome, needed for cropEdges
            MakeMonochrome makeMonochrome = new MakeMonochrome();
            makeMonochrome.DoAlgorithm(ref croppedArray);

            //Rotate the license plate
            //We do not use this at the moment, it makes the result often worse
            //RotateLicensePlate rotateLicensePlate = new RotateLicensePlate("");
            //Bitmap rotateLicensePlateResult = rotateLicensePlate.DoAlgorithm(croppedBitmap);

            //Crops the black edges around the license plate, also filters noise
            CropCharacters cropCharacters = new CropCharacters();
            Bitmap[] characters = cropCharacters.DoAlgorithm(ref croppedArray, croppedBitmap.Width, croppedBitmap.Height);

            //For testing only, do not use this
            //Bitmap monochromeBitmap = new Bitmap(croppedBitmap);
            //monochromeBitmap = MyImage.ArrayToBmp(croppedArray, monochromeBitmap);
            //return monochromeBitmap;

            //Only for testing purposes, do not use this
            //int counter = 0;
            //foreach (Bitmap b in characters)
            //{
            //    b.Save("C:\\test1\\" + counter + ".bmp", System.Drawing.Imaging.ImageFormat.Jpeg);
            //    counter++;
            //}

            //return the first character, only so the project compiles for now            
            if (characters[0] != null)
            {
                return characters[0];
            }
            else
            {
                //MessageBox.Show("Geen bruikbaar resultaat gevonden");
                return null;
            }
        }
    }
}
